package com.qianya.system.face.web.patient.controller;

import cn.hutool.core.util.StrUtil;
import com.qianya.model.common.JWTUtil;
import com.qianya.model.pojo.patient.qo.BoObj;
import com.qianya.model.pojo.patient.qo.PatientOxygenSignQO;
import com.qianya.model.pojo.patient.vo.PatientFileInfoPlanVO;
import com.qianya.system.face.config.RedisUtil;
import com.qianya.system.face.web.patient.model.PatientOxygen;
import com.qianya.system.face.web.patient.service.IPatientOxygenService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qianya.util.BinaryUtil;
import com.qianya.util.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.qianya.model.common.CommonResult;
import com.qianya.model.common.ResultCode;
import com.qianya.model.pojo.patient.qo.PatientOxygenQO;
import com.qianya.model.pojo.patient.vo.PatientOxygenVO;
    import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.*;

/**
* <p>
    *  前端控制器
    * </p>
*
* @author lhl
* @since 2020-08-11
*/
    @RestController
@RequestMapping("/patient/patient-oxygen")
@Api(value = "PatientOxygenController", tags = "体征报告接口")
@Slf4j
public class PatientOxygenController {
    @Autowired
    private IPatientOxygenService patientOxygenService;


    @PostMapping(value = "/save")
    @ApiOperation("获得体征状态")
    public CommonResult save(@Valid @RequestBody BoObj bo, HttpServletRequest request){
       // Integer[] boArray=BinaryUtil.toChange(bo.getBo());
//        String[] boArray2=bo.getBo().split(",");
//        Integer[] boArray=new Integer[2];
//        for (int i = 0; i <boArray2.length ; i++) {
//            boArray[i]=Integer.valueOf(boArray2[i]);
//        }
        Integer[] boArray=BinaryUtil.toChange(bo.getBo());
        if (boArray==null){
            return CommonResult.error(ResultCode.ERROR);
        }
        if (boArray[0]==127 && boArray[1]==255){ //判断是否停止测试体征
            List<Integer[]> list= ((List<Integer[]>)RedisUtil.lget("bo"+bo.getFileId(),0,-1));
            if (list.size()==0){ //重复请求停止测试
                return CommonResult.error(ResultCode.NOSIGNSSTATE,"没有体征状态");
            }else {
                Integer[] oxy=new Integer[list.size()];
                Integer[] rate=new Integer[list.size()];
                for (int i = 0; i <list.size() ; i++) {
                        oxy[i]=list.get(i)[0];
                        rate[i]=list.get(i)[1];
                }
                int oxyVal=numCount(oxy);
                int rateVal=numCount(rate);
                RedisUtil.removeObject("bo"+bo.getFileId()); //清空储存的体征信息
                PatientOxygen po=new PatientOxygen();
                po.setHeartRate(String.valueOf(rateVal));
                po.setBloodOxygen(String.valueOf(oxyVal));
                po.setCreateId(JWTUtil.getUserId(request.getHeader("token")));
                //po.setCreateId("1");
                po.setCreateTime(new Date());
                po.setFileId(bo.getFileId());
                patientOxygenService.save(po);
            }
        }else {
            RedisUtil.lset("bo"+bo.getFileId(),boArray);
        }
        return CommonResult.success();
    }

    @ApiOperation("体征报告查询")
    @GetMapping(value = "/find")
    public CommonResult<IPage<PatientOxygenVO>> find(@Valid PatientOxygenQO patientOxygenQO){
        if (patientOxygenQO.getTime()!=null && !"".equals(patientOxygenQO.getTime()) && !DateUtils.valiDateTimeWithLongFormat(patientOxygenQO.getTime())){
            return CommonResult.error(ResultCode.ERROR,"日期格式不对");
        }
        return patientOxygenService.find(patientOxygenQO);
    }
    @ApiOperation("根据日期查询所有患者体征状态")
    @GetMapping(value = "/patientSign")
    public CommonResult<PatientFileInfoPlanVO> patientSign(@Valid  PatientOxygenSignQO patientOxygenSignQO
    ){
        if (StrUtil.isBlank(patientOxygenSignQO.getTime())){
            patientOxygenSignQO.setTime("");
        }
        if (!"".equals(patientOxygenSignQO.getTime()) && !DateUtils.valiDateTimeWithLongFormat(patientOxygenSignQO.getTime())){
            return CommonResult.error(ResultCode.ERROR,"日期格式不对");
        }
        return patientOxygenService.patientSign(patientOxygenSignQO);
    }
    public static int numCount(Integer[] inter){
        int counts[] = new int[inter.length];// a是对象名
        for (int i = 0; i < inter.length; i++) {
            for (int j = 0; j < inter.length; j++) {
                if (inter[i].equals(inter[j]))
                    counts[i]++;//
            }
        }
        int num= 0;//a[b.length - 1];//
        int countindex = 0;
        // int count=a[0];
        for (int i = 0; i < inter.length - 1; i++) {
            if (num <= counts[i]) {
                num = counts[i];
                countindex = i;// 输出最大值的索引
            }
        }
        int interCount=inter.length;
        if (interCount<=5 && num>=2){
            return inter[countindex];
        }
        Arrays.sort(inter);
        int index=inter.length/4;
        int InterSum=0;
        for (int i = index; i <inter.length-index ; i++) {
            InterSum+=inter[i];
        }
        return InterSum/(inter.length-2*index);
    }
}